//
//  PzwoeNJLCFForgetPassViewControllerVoiWYJ.m
//  Dvjf_ios
//
//  Created by Ji on 2018/9/7.
//  Copyright © 2018年 ski. All rights reserved.
//

#import "PzwoeNJLCFForgetPassViewControllerVoiWYJ.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"
#import "dgXPYNJLCFInputViewjVJFSJ.h"

@interface PzwoeNJLCFForgetPassViewControllerVoiWYJ ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *aSHWXpmsgTextFieldgHtMSl;

@property (nonatomic, strong) UITextField *XkdAJOpassTextFielddZXiwK;

@property (nonatomic, strong) UIButton *msgBtn;

@property (nonatomic, strong) NSTimer *qdZZsdcountTimereVpqYS;
@property (nonatomic, assign) int count;

@property (nonatomic, strong) UIButton *FDTTddnextBtnLAOJQZ;

@property (nonatomic, strong) dgXPYNJLCFInputViewjVJFSJ *yemwlIcodeInputViewEUhmXM;
@property (nonatomic, strong) dgXPYNJLCFInputViewjVJFSJ *vpsdkzpwdInputViewCenLev;


@end

static int const showtime = 60;

@implementation PzwoeNJLCFForgetPassViewControllerVoiWYJ

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self addNavigationBar];
//    self.navigationBar.title = @"找回密码";
    
//    self.view.backgroundColor = QTZ_Gray237;
//    [self configView];
    
    [self setUpLoginUI];
    [self getVerifyCode];
}

- (void)setUpLoginUI {
    self.KWcnnUtitleTipsLabelQLOdiQ.text = @"找回密码";
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"验证短信已发送至手机号    " attributes:@{NSFontAttributeName:FONT(10), NSForegroundColorAttributeName:QTZ_Gray153}];
    NSAttributedString *str = [[NSAttributedString alloc] initWithString:self.UabOWQphoneNumberaYOEQN attributes:@{NSFontAttributeName:FONT(10), NSForegroundColorAttributeName:[UIColor blackColor]}];
    [attStr appendAttributedString:str];
    self.NdgzxmdesLabelTTSqri.attributedText = attStr;
    [self.zNugwOsubmitBtnFCoXrL setTitle:@"完成" forState:UIControlStateNormal];
    
    dgXPYNJLCFInputViewjVJFSJ *yemwlIcodeInputViewEUhmXM = [[dgXPYNJLCFInputViewjVJFSJ alloc] initWithInputType:InputTypeCode];
    yemwlIcodeInputViewEUhmXM.jYmrASinputFieldXQCIYG.delegate = self;
    yemwlIcodeInputViewEUhmXM.jYmrASinputFieldXQCIYG.returnKeyType = UIReturnKeyDone;
    yemwlIcodeInputViewEUhmXM.jYmrASinputFieldXQCIYG.keyboardType = UIKeyboardTypeNumberPad;
    yemwlIcodeInputViewEUhmXM.ajptsrleftTipsImageViewgYkbmv.image = [UIImage imageNamed:@"icon_login_code"];
    [yemwlIcodeInputViewEUhmXM setInputTextPlaceHolder:@"请输入验证码"];
    [self.contentView addSubview:yemwlIcodeInputViewEUhmXM];
    [yemwlIcodeInputViewEUhmXM mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(FitRealValue(78));
        make.right.mas_equalTo(self.contentView.mas_right).offset(-FitRealValue(78));
        make.height.mas_equalTo(FitRealValue(80));
        make.top.mas_equalTo(self.NdgzxmdesLabelTTSqri.mas_bottom).offset(FitRealValue(35));
    }];
    _yemwlIcodeInputViewEUhmXM = yemwlIcodeInputViewEUhmXM;
    [self.zNugwOsubmitBtnFCoXrL addTarget:self action:@selector(handleComplete) forControlEvents:UIControlEventTouchUpInside];
    
    
    dgXPYNJLCFInputViewjVJFSJ *vpsdkzpwdInputViewCenLev = [[dgXPYNJLCFInputViewjVJFSJ alloc] initWithInputType:InputTypeMessage];
    vpsdkzpwdInputViewCenLev.jYmrASinputFieldXQCIYG.delegate = self;
    vpsdkzpwdInputViewCenLev.jYmrASinputFieldXQCIYG.returnKeyType = UIReturnKeyDone;
    vpsdkzpwdInputViewCenLev.jYmrASinputFieldXQCIYG.secureTextEntry = YES;
    vpsdkzpwdInputViewCenLev.ajptsrleftTipsImageViewgYkbmv.image = [UIImage imageNamed:@"icon_login_pwd"];
    [vpsdkzpwdInputViewCenLev setInputTextPlaceHolder:@"请设置新密码"];
    [self.contentView addSubview:vpsdkzpwdInputViewCenLev];
    [vpsdkzpwdInputViewCenLev mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left).offset(FitRealValue(78));
        make.right.mas_equalTo(self.contentView.mas_right).offset(-FitRealValue(78));
        make.height.mas_equalTo(FitRealValue(80));
        make.top.mas_equalTo(yemwlIcodeInputViewEUhmXM.mas_bottom).offset(FitRealValue(30));
    }];
    _vpsdkzpwdInputViewCenLev = vpsdkzpwdInputViewCenLev;
    
    vpsdkzpwdInputViewCenLev.clicked = ^{
        
    };
    
    yemwlIcodeInputViewEUhmXM.oswKUxrightBtnlkMbAe.userInteractionEnabled = NO;
    [yemwlIcodeInputViewEUhmXM.oswKUxrightBtnlkMbAe addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];

}

- (void)configView
{
    
    UILabel *titleLabel = [UILabel labelWithText:@"找回密码" textColor:QTZ_Gray51 font:FONT(25)];
    titleLabel.left = 40;
    titleLabel.top = NAVBAR_HEIGHT + 75;
    [self.view addSubview:titleLabel];
    
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"验证短信已发送至手机号  %@", self.UabOWQphoneNumberaYOEQN]];
    [attributeStr addAttribute:NSFontAttributeName value:FONT(13) range:NSMakeRange(0, attributeStr.length)];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:QTZ_Gray153 range:NSMakeRange(0, attributeStr.length)];
    [attributeStr addAttribute:NSForegroundColorAttributeName value:QTZ_Gray51 range:NSMakeRange(13, 11)];
    UILabel *edHyphtipsLabelTVYPVW = [UILabel new];
    edHyphtipsLabelTVYPVW.attributedText = attributeStr;
    [edHyphtipsLabelTVYPVW sizeToFit];
    edHyphtipsLabelTVYPVW.left = 40;
    edHyphtipsLabelTVYPVW.top = titleLabel.bottom + 16;
    [self.view addSubview:edHyphtipsLabelTVYPVW];
 
    self.aSHWXpmsgTextFieldgHtMSl = [[UITextField alloc] initWithFrame:CGRectMake(38, edHyphtipsLabelTVYPVW.bottom + 60, FULL_WIDTH - 76, 25)];
    self.aSHWXpmsgTextFieldgHtMSl.font = FONT(16);
    self.aSHWXpmsgTextFieldgHtMSl.delegate = self;
    self.aSHWXpmsgTextFieldgHtMSl.placeholder = @"请输入验证码";
    self.aSHWXpmsgTextFieldgHtMSl.keyboardType = UIKeyboardTypeNumberPad;
    self.aSHWXpmsgTextFieldgHtMSl.returnKeyType = UIReturnKeyDone;
    self.aSHWXpmsgTextFieldgHtMSl.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:self.aSHWXpmsgTextFieldgHtMSl];
    
    self.msgBtn = [UIButton new];
    self.msgBtn.titleLabel.font = FONT(12);
    [self.msgBtn setTitleColor:Theme_Color forState:0];
    [self.msgBtn setTitle:@"获取验证码" forState:0];
    [self.msgBtn sizeToFit];
    [self.msgBtn addTarget:self action:@selector(getVerifyCode) forControlEvents:UIControlEventTouchUpInside];
    self.aSHWXpmsgTextFieldgHtMSl.rightView = self.msgBtn;
    self.aSHWXpmsgTextFieldgHtMSl.rightViewMode = UITextFieldViewModeAlways;
    [self.aSHWXpmsgTextFieldgHtMSl.layer addSublayer:[CALayer addLineOriginPoint:CGPointMake(0.0f, self.aSHWXpmsgTextFieldgHtMSl.frame.size.height) toPoint:CGPointMake(self.aSHWXpmsgTextFieldgHtMSl.frame.size.width, self.aSHWXpmsgTextFieldgHtMSl.frame.size.height) color:QTZ_Gray182 borderWidth:.5]];
    
    
    self.XkdAJOpassTextFielddZXiwK = [[UITextField alloc] initWithFrame:CGRectMake(38, self.aSHWXpmsgTextFieldgHtMSl.bottom + 25, FULL_WIDTH - 76, 25)];
    self.XkdAJOpassTextFielddZXiwK.secureTextEntry = YES;
    self.XkdAJOpassTextFielddZXiwK.font = FONT(16);
    self.XkdAJOpassTextFielddZXiwK.returnKeyType = UIReturnKeyDone;
    self.XkdAJOpassTextFielddZXiwK.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.XkdAJOpassTextFielddZXiwK.delegate = self;
    self.XkdAJOpassTextFielddZXiwK.placeholder = @"请设置新密码";
    [self.view addSubview:self.XkdAJOpassTextFielddZXiwK];
    [self.XkdAJOpassTextFielddZXiwK.layer addSublayer:[CALayer addLineOriginPoint:CGPointMake(0.0f, self.XkdAJOpassTextFielddZXiwK.frame.size.height) toPoint:CGPointMake(self.XkdAJOpassTextFielddZXiwK.frame.size.width, self.XkdAJOpassTextFielddZXiwK.frame.size.height) color:QTZ_Gray182 borderWidth:.5]];
    
    
    self.FDTTddnextBtnLAOJQZ = [[UIButton alloc] initWithFrame:CGRectMake(38, self.XkdAJOpassTextFielddZXiwK.bottom + 35, self.XkdAJOpassTextFielddZXiwK.width, 45)];
    self.FDTTddnextBtnLAOJQZ.titleLabel.font = FONT(12);
    [self.FDTTddnextBtnLAOJQZ setTitleColor:QTZ_White forState:0];
    [self.FDTTddnextBtnLAOJQZ setTitle:@"完成" forState:0];
    [self.FDTTddnextBtnLAOJQZ setBackgroundImage:[UIImage imageNamed:@"icon_mine_btnBg"] forState:0];
    self.FDTTddnextBtnLAOJQZ.layer.cornerRadius = 5;
    [self.FDTTddnextBtnLAOJQZ addTarget:self action:@selector(handleComplete) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.FDTTddnextBtnLAOJQZ];
    
}

//获取验证码
- (void)getVerifyCode
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setSafeObject:self.UabOWQphoneNumberaYOEQN forKey:@"phone"];
    [params setSafeObject:@"3" forKey:@"type"];
    
    [sHkPtEJLCFMineRequestozAYZs postMsgCodeWithParams:params loadingInView:self.view success:^{
        [self handleVerifyBtn];
        [self showNotice:@"验证码发送成功"];
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];
}

- (void)handleVerifyBtn
{
    _count = showtime;
    [[NSRunLoop mainRunLoop] addTimer:self.qdZZsdcountTimereVpqYS forMode:NSRunLoopCommonModes];
}

- (NSTimer *)qdZZsdcountTimereVpqYS
{
    if (!_qdZZsdcountTimereVpqYS) {
        _qdZZsdcountTimereVpqYS = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    }
    return _qdZZsdcountTimereVpqYS;
}

- (void)countDown {
    _count --;
    [self.yemwlIcodeInputViewEUhmXM.oswKUxrightBtnlkMbAe setTitle:[NSString stringWithFormat:@"倒计时%ds",_count] forState:UIControlStateNormal];
    [self.yemwlIcodeInputViewEUhmXM.oswKUxrightBtnlkMbAe setTitleColor:QTZ_Gray173 forState:0];
    self.yemwlIcodeInputViewEUhmXM.oswKUxrightBtnlkMbAe.userInteractionEnabled = NO;
//    [self.msgBtn setTitle:[NSString stringWithFormat:@"倒计时%ds",_count] forState:UIControlStateNormal];
//    [self.msgBtn setTitleColor:QTZ_Gray173 forState:0];
//    self.msgBtn.userInteractionEnabled = NO;
    if (_count == 0) {
        [self.yemwlIcodeInputViewEUhmXM.oswKUxrightBtnlkMbAe setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.yemwlIcodeInputViewEUhmXM.oswKUxrightBtnlkMbAe setTitleColor:Theme_Color forState:0];
        self.yemwlIcodeInputViewEUhmXM.oswKUxrightBtnlkMbAe.userInteractionEnabled = YES;
//        [self.msgBtn setTitle:@"重新获取" forState:UIControlStateNormal];
//        [self.msgBtn setTitleColor:Theme_Color forState:0];
//        self.msgBtn.userInteractionEnabled = YES;
        [self dismiss];
    }
    [self.msgBtn sizeToFit];
}

- (void)dismiss
{
    [self.qdZZsdcountTimereVpqYS invalidate];
    self.qdZZsdcountTimereVpqYS = nil;
}

- (void)handleComplete
{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setSafeObject:self.UabOWQphoneNumberaYOEQN forKey:@"phone"];
    [params setSafeObject:[[NSString stringWithFormat:@"%@%@", [self.vpsdkzpwdInputViewCenLev.jYmrASinputFieldXQCIYG.text MD5], self.UabOWQphoneNumberaYOEQN] MD5] forKey:@"nPassword"];
    [params setSafeObject:self.yemwlIcodeInputViewEUhmXM.jYmrASinputFieldXQCIYG.text forKey:@"code"];
    
    [sHkPtEJLCFMineRequestozAYZs forgetPassWithParams:params loadingInView:self.view success:^{
        [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] popViewControllerAnimated:YES];
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];
    
}

- (BOOL)dZrvUEtextFieldRRHEAtShouldReturn:(UITextField *)dZrvUEtextFieldRRHEAt{
    return [dZrvUEtextFieldRRHEAt resignFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (BOOL)dZrvUEtextFieldRRHEAt:(UITextField *)dZrvUEtextFieldRRHEAt shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (dZrvUEtextFieldRRHEAt == self.yemwlIcodeInputViewEUhmXM.jYmrASinputFieldXQCIYG) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.yemwlIcodeInputViewEUhmXM.jYmrASinputFieldXQCIYG.text.length >= 6) {
            self.yemwlIcodeInputViewEUhmXM.jYmrASinputFieldXQCIYG.text = [dZrvUEtextFieldRRHEAt.text substringToIndex:6];
            return NO;
        }
    }
    
    if (dZrvUEtextFieldRRHEAt == self.vpsdkzpwdInputViewCenLev.jYmrASinputFieldXQCIYG) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.vpsdkzpwdInputViewCenLev.jYmrASinputFieldXQCIYG.text.length >= 15) {
            self.vpsdkzpwdInputViewCenLev.jYmrASinputFieldXQCIYG.text = [dZrvUEtextFieldRRHEAt.text substringToIndex:15];
            return NO;
        }
    }
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
